package com.atguigu.yygh.hosp.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除科室
    @PostMapping("department/remove")
    public Result remove(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        departmentService.removeDeptId(hoscode,depcode);

        return Result.ok();
    }

    //查询科室（条件查询带分页）
    @PostMapping("department/list")
    public Result departmentPage(HttpServletRequest request) {
        Map<String, Object> paramMap =
                HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        //当前页
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        //每页记录数
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        //调用service
        Page<Department> pageModel = departmentService.selectPage(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    //上传科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取医院系统传递map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //把paramMap转换department对象
        //paramMap--json
        String mapStr = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(mapStr, Department.class);

        //调用方法添加
        departmentService.save(department);
        return Result.ok();
    }

    //查询医院接口
    @PostMapping("hospital/show")
    public Result showHosp(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取hoscode医院编号
        String hoscode = (String)paramMap.get("hoscode");

        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(20001,"数据参数错误");
        }

        //TODO  签名校验
        //调用service方法
        Hospital hospital = hospitalService.getHospByHoscode(hoscode);
        //返回对象
        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("saveHospital")
    public Result createHosp(HttpServletRequest request) {
        //1 获取医院模拟系统传递map集合
        // getParameterMap作用获取提交所有参数名称和值，封装map集合

        /**
         *   <input type="" name="name" value="111">
         *   <input type="" name="age" value="20">
         *   <input type="checkbox" name="love" value="吃饭">
         *   <input type="checkbox" name="love" value="睡觉">
         *   key: name   value:111
         *   key: age   value:20
         */
        Map<String, String[]> parameterMap = request.getParameterMap();

        // Map<String, String[]> 变成 Map<String, Object>
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //2 paramMap，添加mongodb
        //paramMap变成对象
        //使用json工具实现： GSON  jackson  fastjson
        //map集合json字符串
        String mapStr = JSONObject.toJSONString(paramMap);
        //json字符串变成对象
        Hospital hospital = JSONObject.parseObject(mapStr, Hospital.class);

        //签名校验
        //把医院系统传过来key获取到
        String hospSign = (String)paramMap.get("sign");

        //查询平台里面存储这个医院key和 传过来key是否一样
        String signKey = hospitalSetService.getHospSetSignKey(hospital.getHoscode());
        String encryptMD5 = MD5.encrypt(signKey);

        if(!hospSign.equals(encryptMD5)) {
            throw new YyghException(20001,"签名校验失败");
        }

        //调用方法添加mongodb
        hospitalService.saveHosp(hospital);

        return Result.ok();
    }

}
